package com.yksj.healthtalk.ui.doctorstation.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.universalimageloader.core.ImageLoader;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.entity.DoctorPatient;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonsfHttpResponseHandler;
import com.yksj.healthtalk.net.http.RequestParams;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.doctorstation.DoctorServiceStatusContent;

/**
 * 
 * 
 * 购买服务人员列表
 * @author zhao
 * 
 *
 */
public class DoctorServiceBuyerListActivity extends BaseFragmentActivity implements OnClickListener {
	
	ListView mListView;
	DataListAdapter mAdapter;
	String mTitle;
	String mServiceItemId;
	String mDate;
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.doctor_service_description_list_layout);
		
		if(arg0 != null){
			mTitle = arg0.getString("mTitle");
			mServiceItemId = arg0.getString("mServiceItemId");
			mDate = arg0.getString("mDate");
		}else{
			mTitle = getIntent().getStringExtra("title");
			mDate = getIntent().getStringExtra("date");
			mServiceItemId = getIntent().getStringExtra("serviceItemId");
		}
		
		initUI();
		onQueryData();
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString("mTitle",mTitle);
		outState.putString("mServiceItemId",mServiceItemId);
		outState.putString("mDate",mDate);
	}
	
	private void initUI(){
		initTitle();
		setTitle("购买者");
		setBackBtn("返回",this);
		mListView = (ListView)findViewById(android.R.id.list);
		mListView.setAdapter(mAdapter = new DataListAdapter(this));
	}
	
	private void onQueryData(){
		/**
		 * 
		 * CUSTOMER_ID 客户id
			ROLE_ID 角色id
			ORDER_ID 订单id
			SERVICE_STATUS 订单状态
			CUSTOMER_ACCOUNTS 多美号
			CUSTOMER_NICKNAME 昵称
			CUSTOMER_SEX 性别
			PERSONAL_NARRATE 个人说明
			BIG_ICON_BACKGROUND 大头像
			CLIENT_ICON_BACKGROUND 小头像
			REMARKS_NAME 备注名字
			PAY_OK_TIME 购买成功实践
		 */
		RequestParams paramsm = new RequestParams();
		paramsm.put("serviceItemId", mServiceItemId);
		paramsm.put("date", mDate);
		paramsm.put("customerId", SmartFoxClient.getLoginUserId());
		HttpRestClient.doHttpFindCustomerByServiceItemId(paramsm,new JsonsfHttpResponseHandler(this){
			@Override
			public void onSuccess(int statusCode, JSONArray response) {
				mAdapter.onDataChange(response);
			}
		});
	}
	
	/**
	 * 
	 * 点击查询
	 * @param object
	 */
	private void onDoQueryData(final JSONObject object){
		RequestParams params = new RequestParams();
		params.put("Type","queryOrderMessage");
		params.put("ORDER_ID",object.getString("ORDER_ID"));
		params.put("CUSTOMER_ID",SmartFoxClient.getLoginUserId());
		HttpRestClient.doHttpServicesetservletrj320(params,new JsonsfHttpResponseHandler(this){
			@Override
			public void onSuccess(int statusCode, JSONObject response) {
				DoctorPatient mPatient=new DoctorPatient();
				mPatient.setServiceStatusCode(response.getString("SERVICE_STATUS"));
				mPatient.setServiceTypeId(response.getString("SERVICE_TYPE_ID"));
				mPatient.setOrderId(response.getString("ORDER_ID"));
				mPatient.setCustomerId(response.getString("SERVICE_CUSTOMER_ID"));
				
				mPatient.setClientIconBackground(object.getString("CLIENT_ICON_BACKGROUND"));
				mPatient.setCustomerNickname(object.getString("CUSTOMER_NICKNAME"));
				mPatient.setCustomerSex(object.getString("CUSTOMER_SEX"));
				Intent intent;
					
					if(mPatient.getServiceStatusCode().equals("175")||mPatient.getServiceStatusCode().equals("180")){
						intent=new Intent(DoctorServiceBuyerListActivity.this,DoctorServiceStatusContent.class);
						intent.putExtra("ORIDERID", mPatient.getOrderId());
						intent.putExtra("CUSTOMER_ID", SmartFoxClient.getLoginUserId());
						intent.putExtra("DOCTORID", mPatient.getCustomerId());
						intent.putExtra("tag", 1000);
						startActivityForResult(intent,5000);
					}else if("1".equals(mPatient.getServiceTypeId())){//普通
						intent=new Intent(DoctorServiceBuyerListActivity.this,DoctorServiceStatusContent.class);
						intent.putExtra("entity", mPatient);
						intent.putExtra("tag", 2000);
						startActivityForResult(intent,5000);
						
					}else if("2".equals(mPatient.getServiceTypeId())||"3".equals(mPatient.getServiceTypeId())||"50".equals(mPatient.getServiceStatusCode())){//预约
						intent=new Intent(DoctorServiceBuyerListActivity.this,DoctorServiceStatusContent.class);
						intent.putExtra("entity", mPatient);
						intent.putExtra("tag", 3000);
						startActivityForResult(intent,5000);
					}
				
			}
		});
		
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			onBackPressed();
			break;
		}
	}
	
	private class DataListAdapter extends BaseAdapter{
		JSONArray mArray = new JSONArray();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		final ImageLoader mImageLoader = ImageLoader.getInstance();
		
		public DataListAdapter(Context context) {
		
		}
		
		public void onDataChange(JSONArray array){
			this.mArray = array;
			notifyDataSetChanged();
		}
		
		@Override
		public int getCount() {
			return mArray.size();
		}

		@Override
		public JSONObject getItem(int position) {
			return mArray.getJSONObject(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			final JSONObject jsonObject = getItem(position);
			final ImageView mImageView;
			final TextView nameTextView;
			final TextView timeTextView;
			final Button button;
			if(convertView == null){
				convertView = getLayoutInflater().inflate(R.layout.service_buyers_list_item,null);
				mImageView = (ImageView)convertView.findViewById(R.id.head_image);
				nameTextView = (TextView)convertView.findViewById(R.id.title);
				timeTextView = (TextView)convertView.findViewById(R.id.title_time);
				button = (Button)convertView.findViewById(R.id.item_btn);
				convertView.setTag(R.id.head_image,mImageView);
				convertView.setTag(R.id.title,nameTextView);
				convertView.setTag(R.id.title_time,timeTextView);
				convertView.setTag(R.id.item_btn,button);
			}else{
				mImageView = (ImageView)convertView.getTag(R.id.head_image);
				nameTextView = (TextView)convertView.getTag(R.id.title);
				timeTextView = (TextView)convertView.getTag(R.id.title_time);
				button = (Button)convertView.getTag(R.id.item_btn);
			}
			String bg = jsonObject.getString("CLIENT_ICON_BACKGROUND");
			String CUSTOMER_ACCOUNTS = jsonObject.getString("CUSTOMER_ACCOUNTS");
			String CUSTOMER_NICKNAME = jsonObject.getString("CUSTOMER_NICKNAME");
			String SERVICE_STATUS = jsonObject.getString("SERVICE_STATUS");
			String PAY_OK_TIME = null;
			if(jsonObject.containsKey("ORDER_CREATE_TIME_LOCAL")){
				PAY_OK_TIME  = jsonObject.getString("ORDER_CREATE_TIME_LOCAL");
			}else{
				try {
					java.util.Date date = format.parse(jsonObject.getString("ORDER_CREATE_TIME"));
					PAY_OK_TIME = "购买时间: "+ format2.format(date);
				} catch (ParseException e) {
					e.printStackTrace();
					PAY_OK_TIME = "购买时间: ";
				}
				jsonObject.put("ORDER_CREATE_TIME_LOCAL",PAY_OK_TIME);
			}
			if(CUSTOMER_NICKNAME != null){
				CUSTOMER_ACCOUNTS = CUSTOMER_NICKNAME;
//				+"("+CUSTOMER_ACCOUNTS+")";
			}
			timeTextView.setText(PAY_OK_TIME);
			nameTextView.setText(CUSTOMER_ACCOUNTS);
			mImageLoader.displayImage(jsonObject.getString("CUSTOMER_SEX"),bg, mImageView);
			switch (Integer.valueOf(SERVICE_STATUS)) {
			case 10:
				button.setText("未支付");
				break;
			case 150:
				button.setText("延时请求");
				break;
			case 60:
				button.setText("退订请求");
				break;
			case 185:
				button.setText("停止服务");
				break;
			case 175:
			case 180:
				button.setText("取消原因");
				break;
			case 50:
			case 70:
			case 160:
			case 170:
			case 155:
				button.setText("查看详情");
				break;
			default:
				if(Integer.valueOf(SERVICE_STATUS)==50){
					button.setText("查看详情");
				}else{
					button.setVisibility(View.INVISIBLE);	
				}
				break;
			}
			button.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					onDoQueryData(jsonObject);
				}
			});
			return convertView;
		}
	}
	
}
